<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View};
use think\exception\ValidateException;

use app\common\model\{Audience, Matchmaker, User, UserLabel, UserNews, Consultant, Tutor, Service};
use app\common\validate\{AudienceValidate, MatchmakerValidate, ConsultantValidate, TutorValidate, ServiceValidate};

class ApplyController extends BaseController
{
    private $userModel;
    public function __construct(User $userModel)
    {
        parent::initialize();
        $this->userModel = $userModel;
    }

    /** 渲染听客申请页面 */
    public function audience_list()
    {
        return view();
    }

    /** 听客申请数据列表 */
    public function audience_data()
    {
        $data = input('get.');

        try{
            validate(AudienceValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }
        }

        $audienceModel = new Audience();

        /** 查询数据总条数 */
        $count = $audienceModel->getNum($where)['data'];

        /** 查询听客列表 */
        $list = $audienceModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $labelModel = new UserLabel();

        foreach($list['data'] as $k => $v){
            $user_info = $this->userModel->getFind(['id' => $v['user_id']], 'nickname');

            if($user_info['code'] != 'ok'){
                unset($list['data'][$k]);
                continue;
            }

            $v['nickname'] = $user_info['data']['nickname'];
            $v['user_label'] = $labelModel->list(['obj_type' => 1, 'obj_id' => $v['id']]);
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 听客申请通过 */
    public function audience_pass()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(AudienceValidate::class)->scene('admin_show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $audienceModel = new Audience();

        $audience_info = $audienceModel->getFind(['id' => $data['id']], 'id, status, user_id');

        if($audience_info['code'] != 'ok' && $audience_info['data']->getData('status') != 1){
            $this->error_back('数据错误');
        }

        $data['status'] = 2;
        $data['finish_time'] = time();

        $flag = $audienceModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 修改用户为听客 */
        $this->userModel->edit(['id' => $audience_info['data']['user_id'], 'is_audience' => 1]);

        $this->success_back('成功');
    }

    /** 听客申请驳回 */
    public function audience_back()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(AudienceValidate::class)->scene('admin_back')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $audienceModel = new Audience();

        $data['status']      = 3;
        $data['back_msg']    = $data['back_msg'];
        $data['finish_time'] = time();

        $flag = $audienceModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }




    /** 渲染红娘申请页面 */
    public function matchmaker_list()
    {
        return view();
    }

    /** 红娘申请数据列表 */
    public function matchmaker_data()
    {
        $data = input('get.');

        try{
            validate(MatchmakerValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname']) && empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }

            if(empty($data['key']['nickname']) && !empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['phone', 'like', '%' . $data['key']['phone'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }

            if(!empty($data['key']['nickname']) && !empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%'], ['phone', 'like', '%' . $data['key']['phone'] . '%']], 'id')['data'];
                
                $where[] = ['user_id', 'in', $user_ids];
            }
        }

        $userNewsModel   = new UserNews();
        $matchmakerModel = new Matchmaker();

        /** 查询数据总条数 */
        $count = $matchmakerModel->getNum($where)['data'];

        /** 查询红娘申请列表 */
        $list = $matchmakerModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        foreach($list['data'] as $k => $v){
            $user_info = $this->userModel->getFind(['id' => $v['user_id']], 'nickname, phone');

            if($user_info['code'] != 'ok'){
                unset($list['data'][$k]);
                continue;
            }

            /** 查询用户资料 */
            $user_news = $userNewsModel->getFind(['user_id' => $v['user_id']], 'birthday, sex')['data'];

            $v['nickname'] = $user_info['data']['nickname'];
            $v['phone']    = $user_info['data']['phone'];
            $v['age']      = howOld($user_news['birthday']);
            $v['sex']      = $user_news['sex'];
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 红娘申请通过 */
    public function matchmaker_pass()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(MatchmakerValidate::class)->scene('admin_show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $matchmakerModel = new Matchmaker();

        $info = $matchmakerModel->getFind(['id' => $data['id']], 'id, status, user_id');

        if($info['code'] != 'ok' && $info['data']->getData('status') != 1){
            $this->error_back('数据错误');
        }

        $data['status'] = 2;
        $data['finish_time'] = time();

        $flag = $matchmakerModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 修改用户为红娘 */
        $this->userModel->edit(['id' => $info['data']['user_id'], 'is_matchmaker' => 1]);

        $this->success_back('成功');
    }

    /** 红娘申请驳回 */
    public function matchmaker_back()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(MatchmakerValidate::class)->scene('admin_back')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $matchmakerModel = new Matchmaker();

        $data['status']      = 3;
        $data['back_msg']    = $data['back_msg'];
        $data['finish_time'] = time();

        $flag = $matchmakerModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }




    /** 渲染咨询师申请页面 */
    public function consultant_list()
    {
        return view();
    }

    /** 咨询师申请数据列表 */
    public function consultant_data()
    {
        $data = input('get.');

        try{
            validate(ConsultantValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname']) && empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }

            if(empty($data['key']['nickname']) && !empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['phone', 'like', '%' . $data['key']['phone'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }

            if(!empty($data['key']['nickname']) && !empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%'], ['phone', 'like', '%' . $data['key']['phone'] . '%']], 'id')['data'];
                
                $where[] = ['user_id', 'in', $user_ids];
            }
        }

        $consultantModel = new Consultant();

        /** 查询数据总条数 */
        $count = $consultantModel->getNum($where)['data'];

        /** 查询咨询师列表 */
        $list = $consultantModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $labelModel = new UserLabel();

        foreach($list['data'] as $k => $v){
            $user_info = $this->userModel->getFind(['id' => $v['user_id']], 'nickname, phone');

            if($user_info['code'] != 'ok'){
                unset($list['data'][$k]);
                continue;
            }

            $v['nickname'] = $user_info['data']['nickname'];
            $v['phone']    = $user_info['data']['phone'];
            $v['user_label'] = $labelModel->list(['obj_type' => 3, 'obj_id' => $v['id']]);
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 咨询师申请通过 */
    public function consultant_pass()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(ConsultantValidate::class)->scene('admin_show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $consultantModel = new Consultant();

        $info = $consultantModel->getFind(['id' => $data['id']], 'id, status, user_id');

        if($info['code'] != 'ok' && $info['data']->getData('status') != 1){
            $this->error_back('数据错误');
        }

        $data['status'] = 2;
        $data['finish_time'] = time();

        $flag = $consultantModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 修改用户为咨询师 */
        $this->userModel->edit(['id' => $info['data']['user_id'], 'is_consultant' => 1]);

        $this->success_back('成功');
    }

    /** 咨询师申请驳回 */
    public function consultant_back()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(ConsultantValidate::class)->scene('admin_back')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $consultantModel = new Consultant();

        $data['status']      = 3;
        $data['back_msg']    = $data['back_msg'];
        $data['finish_time'] = time();

        $flag = $consultantModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }




    /** 渲染婚恋导师申请页面 */
    public function tutor_list()
    {
        return view();
    }

    /** 婚恋导师申请数据列表 */
    public function tutor_data()
    {
        $data = input('get.');

        try{
            validate(TutorValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname']) && empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }

            if(empty($data['key']['nickname']) && !empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['phone', 'like', '%' . $data['key']['phone'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }

            if(!empty($data['key']['nickname']) && !empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%'], ['phone', 'like', '%' . $data['key']['phone'] . '%']], 'id')['data'];
                
                $where[] = ['user_id', 'in', $user_ids];
            }
        }

        $tutorModel = new Tutor();

        /** 查询数据总条数 */
        $count = $tutorModel->getNum($where)['data'];

        /** 查询婚恋导师列表 */
        $list = $tutorModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $labelModel = new UserLabel();

        foreach($list['data'] as $k => $v){
            $user_info = $this->userModel->getFind(['id' => $v['user_id']], 'nickname, phone');

            if($user_info['code'] != 'ok'){
                unset($list['data'][$k]);
                continue;
            }

            $v['nickname'] = $user_info['data']['nickname'];
            $v['phone']    = $user_info['data']['phone'];
            $v['user_label'] = $labelModel->list(['obj_type' => 3, 'obj_id' => $v['id']]);
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 婚恋导师申请通过 */
    public function tutor_pass()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(TutorValidate::class)->scene('admin_show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $tutorModel = new Tutor();

        $info = $tutorModel->getFind(['id' => $data['id']], 'id, status, user_id');

        if($info['code'] != 'ok' && $info['data']->getData('status') != 1){
            $this->error_back('数据错误');
        }

        $data['status'] = 2;
        $data['finish_time'] = time();

        $flag = $tutorModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 修改用户为婚恋导师 */
        $this->userModel->edit(['id' => $info['data']['user_id'], 'is_tutor' => 1]);

        $this->success_back('成功');
    }

    /** 婚恋导师申请驳回 */
    public function tutor_back()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(TutorValidate::class)->scene('admin_back')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $tutorModel = new Tutor();

        $data['status']      = 3;
        $data['back_msg']    = $data['back_msg'];
        $data['finish_time'] = time();

        $flag = $tutorModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }





    /** 渲染服务站申请页面 */
    public function service_list()
    {
        return view();
    }

    /** 服务站申请数据列表 */
    public function service_data()
    {
        $data = input('get.');

        try{
            validate(ServiceValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname']) && empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }

            if(empty($data['key']['nickname']) && !empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['phone', 'like', '%' . $data['key']['phone'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }

            if(!empty($data['key']['nickname']) && !empty($data['key']['phone'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%'], ['phone', 'like', '%' . $data['key']['phone'] . '%']], 'id')['data'];
                
                $where[] = ['user_id', 'in', $user_ids];
            }
        }

        $serviceModel = new Service();

        /** 查询数据总条数 */
        $count = $serviceModel->getNum($where)['data'];

        /** 查询服务站申请列表 */
        $list = $serviceModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        foreach($list['data'] as $k => $v){
            $user_info = $this->userModel->getFind(['id' => $v['user_id']], 'nickname, phone, id_card');

            if($user_info['code'] != 'ok'){
                unset($list['data'][$k]);
                continue;
            }

            $v['nickname'] = $user_info['data']['nickname'];
            $v['phone']    = $user_info['data']['phone'];
            $v['id_card']  = $user_info['data']['id_card'];
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 服务站申请通过 */
    public function service_pass()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(ServiceValidate::class)->scene('admin_show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $serviceModel = new Service();

        $info = $serviceModel->getFind(['id' => $data['id']], 'id, status, user_id');

        if($info['code'] != 'ok' && $info['data']->getData('status') != 1){
            $this->error_back('数据错误');
        }

        $data['status'] = 2;
        $data['finish_time'] = time();

        $flag = $serviceModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        /** 修改用户为已开通服务站 */
        $this->userModel->edit(['id' => $info['data']['user_id'], 'is_service' => 1]);

        $this->success_back('成功');
    }

    /** 服务站申请驳回 */
    public function service_back()
    {
        $data = input('post.');

        // 验证数据
        try{
            validate(ServiceValidate::class)->scene('admin_back')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $serviceModel = new Service();

        $data['status']      = 3;
        $data['back_msg']    = $data['back_msg'];
        $data['finish_time'] = time();

        $flag = $serviceModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }
}
